﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class entry_teacher : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string Tno = TextBox1.Text;
        string Tname = TextBox2.Text;
        string course1 = TextBox3.Text;
        string course2 = TextBox4.Text;
        string course3 = TextBox5.Text;
        string course4 = TextBox6.Text;
        string dept = DropDownList1.Text;

        string conn = "Data Source= . ;Initial Catalog=XSXK; User ID=sa; pwd=123";
        SqlConnection connection = new SqlConnection(conn);

        try
        {
            if (string.IsNullOrEmpty(Tno) || string.IsNullOrEmpty(Tname) || string.IsNullOrEmpty(course1) || string.IsNullOrEmpty(dept) )
            {
                Response.Write("<h1>添加失败，教师的教师号、姓名、课程1、学院不能为空！！</h1>");
            }
            else
            {

                connection.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = connection;


                string str_sql = "insert into Teacher(Tno , Tname , course1 , course2 ,course3 , course4 , dept) values(  '" + Tno + "' ,  '" + Tname + "', '" + course1 + "'  ,  '" + course2 + "'  , '" + course3 + "'  , '" + course4 + "'  ,  '" + dept + "' )  ";
                comm.CommandText = str_sql;
                //comm.ExecuteScalar();  //如果为查询语句，则用ExecuteScalar()；否则，用ExecuteNonQuery()
                comm.ExecuteNonQuery();
                Response.Write("<h1>成功添加教师信息！</h1>");

            }
        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message);
        }

        connection.Close();//关闭数据库连接
    }
}